<template>
  <div class="info-right">
    <div class="eq-right-top">
      <div class="eq-right-top-title">
        <img src="../../../assets/images/home-page/eq-right.png" alt="" />
        设备信息
      </div>
      <div class="eq-right-top-item">
        <div class="tab-switcher">
          <div class="tab-headers">
            <div
              class="button"
              v-for="(tab, index) in tabs"
              :key="index"
              :class="{ active: givActiveIndex === index }"
              @click="givSwitchTab(index)"
            >
              {{ tab }}
            </div>
          </div>
          <div class="tab-content">
            <div class="tab-i-content-g">
              <div class="tab-g-c-b">
                <div v-for="(item, index) in alarmList" class="tab-g-c-box" @click="showDetails(item, index)">
                  <div class="tab-g-b-box">
                    <div class="t-g-c-img"><img :src="alarmIcon(item.alarmLevel)" alt="" /></div>
                    <div class="t-g-c-box" :class="`alarm-level-${item.alarmLevel}`"
                      ><span class="tab-lab">{{ item.alarmDesc }}：</span>
                      <span class="tab-value">{{ item.createTime }}</span>
                    </div>
                  </div>
                  <div class="tab-g-b-box-d">
                    <span class="spna-o">详情：</span>
                    <span class="span-t">{{ item.alarmDetails }}</span>
                  </div>
                </div>
              </div>
              <!-- <div class="mont-right-bottom" v-if="typeCode">
                <div class="i-r-b-box">
                  <div class="i-r-box-top">
                    <div class="eq-r-t-i-title">
                      <span class="eq-dio"></span>
                      <span class="eq-d-title">视频</span>
                    </div>
                  </div>

                  <div class="tab-headers">
                    <img @click="switchData(-1)" src="../../../assets/images/home-page/m-left.png" alt="" />
                    <div class="buttons">
                      {{ currentItem.videoName }}
                    </div>
                    <img @click="switchData(1)" src="../../../assets/images/home-page/m-right.png" alt="" />
                  </div>
                  <div class="video-box">
                    <NcVideoPlayer ref="myNodeClick" :videoBoj="videoBojct" />
                  </div>
                </div>
              </div> -->
              <div class="tab-i-btn-box">
                <div></div>
                <div class="tab-i-btn" @click="toChange()"> 告警历史 </div>
              </div>
            </div>
          </div>
        </div>
      </div>
    </div>
  </div>
</template>

<script setup>
  import { onMounted, ref, computed, nextTick, onUnmounted } from 'vue';
  import { busDeviceAlarmInfo, busVideo } from '@/api/auth/ledger.js';
  import { getWarningInfo } from '../../../utils/ue.js';
  import NcVideoPlayer from '@/views/home/components/video/NcVideoPlayer.vue';
  import hong from '@/assets/images/home-page/gj-hong.png';
  import huang from '@/assets/images/home-page/gj-huang.png';
  import lan from '@/assets/images/home-page/cheng.png';
  import { tagsStore } from '@/stores';
  const authNav = tagsStore();
  const emit = defineEmits();
  const props = defineProps(['contentList', 'runList', 'deviceCode']);
  const activeIndex = ref(0); // 默认显示第一个选项卡
  const givActiveIndex = ref(0);
  const tabs = ['全部', '一般', '严重', '危急'];
  const alarmList = ref([]);
  const detailsObj = ref({});
  const currentIndex = ref(0);
  const videoBoj = ref([]);
  const videoBojct = ref({});
  const myNodeClick = ref(null);
  const currentItem = computed(() => videoBoj.value[currentIndex.value]);

  const paramsVideo = ref({
    deviceCode: ''
  });

  const tabHeight = ref('69vh');
  const typeCode = ref(false);
  const params1 = ref({
    alarmContent: '',
    alarmDesc: '',
    alarmLevel: '',
    alarmStatus: '',
    alarmSystem: '',
    deviceCode: '',
    deviceName: '',
    orderByDesc: '',
    pageNum: 0,
    pageSize: 0,
    pointId: '',
    pointName: ''
  });
  function alarmIcon(level) {
    const icons = {
      1: huang,
      2: lan,
      3: hong
    };
    return icons[level] || icons['3']; // 默认图标
  }
  const getBusVideo = () => {
    paramsVideo.value.deviceCode = authNav.deviceCode;
    busVideo(paramsVideo.value).then((res) => {
      videoBoj.value = res.data.data;
      videoBojct.value = res.data.data[0];
      console.log('videoBoj.value:', videoBoj.value);
      console.log('res', res);
    });
  };
  const getBusDeviceAlarmInfo = () => {
    console.log('props', props.deviceCode.value);
    // params1.value.deviceCode = authNav.deviceCode;
    busDeviceAlarmInfo(params1.value).then((res) => {
      alarmList.value = res.data.data;
      console.log('res', res);
    });
  };
  function switchData(direction) {
    // 在 nextTick 外计算新索引
    videoBojct.value = {};
    let newIndex = currentIndex.value + direction;

    // 使用 nextTick 等待 DOM 更新
    nextTick(() => {
      // 检查新索引是否在有效范围内
      console.log('videoBoj.value:', videoBoj.value);
      if (newIndex >= 0 && newIndex < videoBoj.value.length) {
        currentIndex.value = newIndex; // 更新索引
        videoBojct.value = currentItem.value;

        if (myNodeClick.value) {
          myNodeClick.value.handleNodeClick(currentItem.value);
          console.log('已切换到新数据:', currentItem.value); // 打印当前项以验证
        }
        // console.log('已切换到新数据:', currentItem.value); // 打印当前项以验证
      } else {
        // 处理边界情况，例如禁用按钮或显示提示
        console.log('已到达列表的开始或结束');
      }
    });
  }

  function handleDataChange(data) {
    // 处理当前数据
    console.log('当前数据:', data);
  }
  function switchTab(index) {
    activeIndex.value = index;
    emit('switchTab', index);
  }
  function givSwitchTab(index) {
    givActiveIndex.value = index;
    if (index === 0) {
      params1.value.alarmLevel = '';
      getBusDeviceAlarmInfo();
    } else {
      params1.value.alarmLevel = index;
      getBusDeviceAlarmInfo();
    }
  }
  function canChange(e) {
    typeCode.value = e;
  }
  function toChange() {
    emit('toChange');
  }
  function showDetails(item, index) {
    typeCode.value = true;
    // getBusVideo();
    nextTick(() => {
      console.log('1111111', myNodeClick.value);
      if (myNodeClick.value) {
        console.log('1111111', videoBoj.value);
        myNodeClick.value.handleNodeClick(videoBoj.value[0]);
      }
    });
    tabHeight.value = '40vh';
    item.deviceName = 'DS___313_2'
    getWarningInfo(item);
  }
  const xjChange = () => {
    console.log('生成图表');
    emit('xjChange');
  };
  onMounted(() => {
    console.log('authNav', authNav.deviceCode);
    getBusDeviceAlarmInfo();
    getBusVideo();
  });
  // 确保在组件卸载时销毁图表实例
  onUnmounted(() => {});
</script>

<style lang="scss" scoped>
  .info-right {
    width: 360px;
    position: absolute;
    top: 120px;
    right: 18px;
    z-index: 999;
    .eq-right-top {
      width: 100%;
      // margin-top: 15px;
      .eq-right-top-title {
        width: 100%;
        height: 33px;
        background: rgba(10, 39, 82, 1);
        display: flex;
        align-items: center;
        color: #fff;
        font-family: Microsoft YaHei;
        font-weight: 400;
        font-size: 16px;
        img {
          width: 20px;
          height: 21px;
          margin: 0 5px 0 10px;
        }
      }
      .eq-right-top-item {
        // height: 504px;
        width: 100%;
        background: rgba(5, 30, 67, 0.6);
        padding-bottom: 13px;
        .tab-switcher {
          width: 327px;
          margin: 0 auto;
          .tab-headers {
            width: 227px;
            height: 32px;
            display: flex;
            align-items: center;
            justify-content: space-between;
            .button {
              cursor: pointer;
              width: 46px;
              height: 22px;
              font-family: PingFang SC;
              font-size: 12px;
              color: #ffffff;
              display: flex;
              align-items: center;
              justify-content: center;
              &.active {
                background: linear-gradient(0deg, #0b8ccf, #07268a);
                border-radius: 4px;
              }

              &:hover {
                opacity: 0.95;
              }
            }
          }
          .tab-i-content-g {
            .tab-g-c-b {
              width: 100%;
              margin: 6px auto 0 auto;
              overflow-y: scroll;
              .tab-g-c-box {
                width: 308px;
                height: 105px;
                background-image: url('../../../assets/images/home-page/gj-bg.png');
                background-size: 100% 100%;
                margin: 0 auto;
                margin-bottom: 5px;
                &:hover {
                  opacity: 0.9;
                  cursor: pointer;
                }
                .tab-g-b-box-d {
                  width: 85%;
                  display: flex;
                  align-items: center;
                  margin: 0 auto;
                  span {
                    margin-top: 5px;
                    display: block;
                    font-family: PingFang SC;
                    font-size: 12px;
                    color: #ffffff;
                  }
                  .spna-o {
                    width: 15%;
                  }
                  .span-t {
                    width: 85%;
                  }
                }
                .tab-g-b-box {
                  width: 100%;
                  display: flex;
                  align-items: center;
                  padding-top: 10px;
                  .t-g-c-box {
                    span {
                      display: block;
                      font-family: PingFang SC;
                      font-size: 14px;
                    }
                    .tab-value {
                      margin-top: 10px;
                    }
                  }
                  .alarm-level-1 {
                    .tab-lab {
                      color: #ffc148; /* 等级1的描述颜色 */
                    }
                  }

                  .alarm-level-2 {
                    .tab-lab {
                      color: #ed7b2f; /* 等级2的描述颜色 */
                    }
                  }

                  .alarm-level-3 {
                    .tab-lab {
                      color: #f2634f; /* 等级3的描述颜色 */
                    }
                  }

                  .alarm-level-1 {
                    .tab-value {
                      color: #ffc148; /* 等级1的时间颜色 */
                    }
                  }

                  .alarm-level-2 {
                    .tab-value {
                      color: #ed7b2f; /* 等级2的时间颜色 */
                    }
                  }

                  .alarm-level-3 {
                    .tab-value {
                      color: #f2634f; /* 等级3的时间颜色 */
                    }
                  }
                  .t-g-c-img {
                    width: 29px;
                    height: 28px;
                    margin: 0 20px;
                    img {
                      width: 100%;
                      height: 100%;
                    }
                  }
                }
              }
              .min {
                height: 65px;
              }
            }
            .tab-min {
              height: 40vh;
            }
            .mont-right-bottom {
              width: 100%;
              margin: 0 auto;

              .i-r-b-box {
                .i-r-box-top {
                  display: flex;
                  align-items: center;
                  .eq-r-t-i-title {
                    width: 100px;
                    display: flex;
                    align-items: center;
                    padding-top: 10px;
                    .eq-dio {
                      width: 8px;
                      height: 8px;
                      background: #19db3c;
                      border-radius: 50%;
                    }
                    .eq-d-title {
                      font-family: PingFang SC;
                      font-weight: bold;
                      font-size: 14px;
                      color: #ffffff;
                      text-shadow: 0px 2px 3px rgba(0, 0, 0, 0.5);
                      margin-left: 5px;
                    }
                  }
                  .tab-i-btn-box {
                    width: 324px;
                    display: flex;
                    justify-content: space-between;
                    margin: 0 auto;
                    .tab-i-btn {
                      width: 80px;
                      height: 30px;
                      background-image: url('../../../assets/images/home-page/tubiao-bg.png');
                      background-size: 100% 100%;
                      display: flex;
                      align-items: center;
                      justify-content: center;
                      font-family: PingFang SC;
                      font-size: 12px;
                      color: #ffffff;
                      // margin: 10px auto 0 auto;
                      cursor: pointer;
                      &:hover {
                        opacity: 0.95;
                      }
                    }
                  }
                }
                .tab-headers {
                  width: 337px;
                  height: 32px;
                  padding: 0 10px;
                  margin: 0 auto;
                  background-image: url('../../../assets/images/home-page/info-bg.png');
                  background-size: 100% 100%;
                  display: flex;
                  align-items: center;
                  justify-content: space-around;
                  img {
                    display: block;
                    width: 12.4px;
                    height: 13.6px;
                    cursor: pointer;
                    &:hover {
                      opacity: 0.95;
                    }
                  }
                  .buttons {
                    cursor: pointer;
                    width: 164px;
                    height: 100%;
                    font-family: PingFang SC;
                    font-size: 12px;
                    color: #ffffff;
                    text-shadow: 0px 2px 3px #006767;
                    display: flex;
                    align-items: center;
                    justify-content: center;
                    // &.active {
                    //   background-image: url('../../../assets/images/home-page/info-tab.png');
                    //   background-size: 100% 100%;
                    // }

                    // &:hover {
                    //   opacity: 0.95;
                    // }
                  }
                }
                .video-box {
                  width: 327px;
                  height: 216px;
                  margin: 0 auto;
                  background-image: url('../../../assets/images/home-page/video-bg.png');
                  background-size: 100% 100%;
                  display: flex;
                  align-items: center;
                  justify-content: center;
                  margin-left: 6px;
                  img {
                    display: block;
                    width: 100%;
                    height: 100%;
                  }
                  img {
                    width: 100%;
                    height: 100%;
                  }
                }
              }
            }
            .tab-i-btn-box {
              width: 96%;
              display: flex;
              justify-content: space-between;
              margin: 10px auto 0 auto;
              .tab-i-btn {
                width: 80px;
                height: 30px;
                background-image: url('../../../assets/images/home-page/tubiao-bg.png');
                background-size: 100% 100%;
                display: flex;
                align-items: center;
                justify-content: center;
                font-family: PingFang SC;
                font-size: 12px;
                color: #ffffff;
                // margin: 10px auto 0 auto;
                cursor: pointer;
                &:hover {
                  opacity: 0.95;
                }
              }
            }
            .eq-r-t-i-title {
              display: flex;
              align-items: center;
              padding-bottom: 10px;
              .eq-dio {
                width: 8px;
                height: 8px;
                background: #19db3c;
                border-radius: 50%;
              }
              .eq-d-title {
                font-family: PingFang SC;
                font-weight: bold;
                font-size: 14px;
                color: #ffffff;
                text-shadow: 0px 2px 3px rgba(0, 0, 0, 0.5);
                margin-left: 5px;
              }
            }
            .tab-content-box {
              width: 98%;
              height: 30vh;
              background-color: rgba(5, 30, 67, 0.65);
              margin: 6px auto 0 auto;
              border: 1px solid #1f4996;
              .tab-i-content {
                width: 100%;
                height: 80%;
                overflow-y: scroll;
                .tab-i-c-box:nth-child(2n) {
                  background-color: rgba(33, 66, 124, 0.3);
                }
                .tab-i-c-box {
                  width: 100%;
                  display: flex;
                  align-items: center;
                  height: 36px;
                  background-color: rgba(10, 138, 206, 0.3);
                  .tab-lab {
                    width: 40%;
                    font-family: PingFang SC;
                    font-size: 16px;
                    color: rgba(255, 255, 255, 0.8);
                    margin-left: 5px;
                    font-size: 12px;
                  }
                  .tab-value {
                    width: 60%;
                    font-family: PingFang SC;
                    font-size: 16px;
                    color: #ffffff;
                    font-size: 12px;
                  }
                }
                // padding: 20px;
                // border: 1px solid #ccc;
                // display: none;
              }
            }
          }
          .tab-content-box-d {
            width: 98%;
            background-color: rgba(5, 30, 67, 0.65);
            border: 1px solid #1f4996;
            margin: 0 auto;
            .tab-i-content {
              width: 100%;
              height: 80%;
              margin: 0 auto;
              .tab-i-c-box:nth-child(2n) {
                background-color: rgba(33, 66, 124, 0.3);
              }
              .tab-i-c-box {
                width: 100%;
                display: flex;
                align-items: center;
                height: 36px;
                background-color: rgba(10, 138, 206, 0.3);
                .tab-lab {
                  width: 30%;
                  font-family: PingFang SC;
                  font-size: 16px;
                  color: rgba(255, 255, 255, 0.8);
                  margin-left: 5px;
                  font-size: 12px;
                }
                .tab-value {
                  width: 70%;
                  font-family: PingFang SC;
                  font-size: 16px;
                  color: #ffffff;
                  font-size: 12px;
                }
              }
              // padding: 20px;
              // border: 1px solid #ccc;
              // display: none;
            }
          }
        }
        .info-right-bottom {
          width: 327px;
          margin: 0 auto;

          .i-r-b-box {
            width: 98%;
            background-color: rgba(5, 30, 67, 0.65);
            border: 1px solid #1f4996;
            height: 41px;
            margin: 6px auto 0 auto;
            display: flex;
            align-items: center;
            justify-content: space-between;
            .i-r-b-left {
              width: 50%;
              display: flex;
              align-items: center;
              justify-content: space-around;
              .right-img {
                width: 13.3px;
                height: 15px;
                cursor: pointer;
                &:hover {
                  opacity: 0.95;
                }
              }
              .sxt-img {
                width: 16.2px;
                height: 18px;
              }
              span {
                font-family: PingFangSC;
                font-weight: 400;
                font-size: 14px;
                color: #ffffff;
              }
            }
            .i-r-b-right {
              width: 15%;
              font-family: PingFangSC;
              font-weight: 400;
              font-size: 14px;
              color: #69feda;
              cursor: pointer;
              &:hover {
                opacity: 0.95;
              }
            }
          }
        }
      }
    }
  }
</style>
